﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CNotebook;
using CNotebook.Purchases;

namespace NotebookS2
{
    public partial class FND_CASHDISBURSE_AP_VOUCHER : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public FND_CASHDISBURSE_AP_VOUCHER()
        {
            InitializeComponent();
        }

        public string ConnectionString { get; set; }
        public string SelectedDocumentNumber { get; set; }

        private void FND_AP_COUNTER_Load(object sender, EventArgs e)
        {
            cboSearch.SelectedIndex = 0;
        }

        private void txtSearch_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            if (cboSearch.Text == "Document Number")
            {
                APVoucherManager oManager = new APVoucherManager();
                DataTable dt = new DataTable();
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                dt = oManager.GetPostedAPVoucherByDocNum(txtSearch.Text);
                oManager.Close();

                gridControl1.DataSource = dt;
                gridControl1.Refresh();
            }
            else
            {
                APCounterManager oManager = new APCounterManager();
                DataTable dt = new DataTable();
                oManager.ConnectionString = this.ConnectionString;
                oManager.Open();

                dt = oManager.GetPostedCounterByVendor(txtSearch.Text);
                oManager.Close();

                gridControl1.DataSource = dt;
                gridControl1.Refresh();
            }

        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            this.SelectedDocumentNumber = gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["DocNum"]).ToString();

            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.SelectedDocumentNumber = "";
            this.Close();
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            btnChoose_Click(sender, e);
        }

        private void txtSearch_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (cboSearch.Text == "Document Number")
                {
                    APVoucherManager oManager = new APVoucherManager();
                    DataTable dt = new DataTable();
                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    dt = oManager.GetAPVoucherByDocNum(txtSearch.Text);
                    oManager.Close();

                    gridControl1.DataSource = dt;
                    gridControl1.Refresh();
                }
                else
                {
                    APVoucherManager oManager = new APVoucherManager();
                    DataTable dt = new DataTable();
                    oManager.ConnectionString = this.ConnectionString;
                    oManager.Open();

                    dt = oManager.GetAPVoucherByVendor(txtSearch.Text);
                    oManager.Close();

                    gridControl1.DataSource = dt;
                    gridControl1.Refresh();
                }

            }
        }

        private void gridControl1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnChoose_Click(sender, e);
            }
        }
    }
}
